<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var people = {
      _name: undefined,
      sex: '男',
      get name() {
        console.log('获取name属性' + this._name);
        return this._name
      },
      set name(val) {
        console.log('为name属性进行赋值操作');
        this._name = val
      }
    }
    //数据属性
    Object.defineProperty(people, 'age', {
      configurable: true,
      enumerable: false,
      value: 18,
      writable: true
    })
    //访问器属性
    Object.defineProperty(people, '_age', {
      configurable: true,
      enumerable: false,
      get: function () {
        console.log('获取age的值' + this.age);
      },
      set: function (val) {
        this.age = val
      }
    })
    console.log(Object.keys(people));
    console.log(Object.getOwnPropertyNames(people));
    //定义多个属性
    Object.defineProperties(people, {
      married: {
        configurable: true,
        enumerable: true,
        value: false,
        writable: true
      },
      hobby: {
        configurable: true,
        enumerable: true,
        value: ['乒乓球', '跑步', '健身', '听歌'],
        writable: true
      }
    })
    console.log(Object.getOwnPropertyNames(people));
    console.log(people);
    //获取对象属性描述
    console.log(Object.getOwnPropertyDescriptor(people, 'hobby'));
    console.log(Object.getOwnPropertyDescriptors(people));
  </script>
</body>

</html>